package com.pignest.hotel.schedule;

import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.pignest.framework.util.Log;
import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.definition.BookingStatus;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;

public class BookingUpdateJob implements Job {
	
	private static final int MISTAKE = 10 * 60 * 1000;

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		Log.v();
//		int h = Config.getBookingKeepHours();
//		Date time = new Date(System.currentTimeMillis() - h * DateUtil.ONE_HOUR);
		BookingStatus status = BookingStatus.ONGOING;
		List<Booking> bookings = Services.getReceptionService().listBookings(status);
		if (bookings == null || bookings.isEmpty())
			return;
		List<Booking> overtimes = new ArrayList<Booking>();
		long now = System.currentTimeMillis();
		for (Booking booking : bookings) {
			if (booking.getEstimatedTime().getTime() + booking.getPreserve() * 60000 + MISTAKE < now) {
				booking.setStatus(BookingStatus.OVERTIME.name());
				overtimes.add(booking);
			}
		}
		if (overtimes.isEmpty())
			return;
		Services.getReceptionService().saveAll(overtimes);
		L.update(overtimes, "系统自动更新");
//		L.log(L.Action.UPDATE, L.getBeanName(Booking.class), "系统自动更新了" + overtimes.size() + "条记录", null);
		Log.i(overtimes.size() + " booking overtime");
	}

}
